package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.HisNursingDto;
import com.woniu.dto.PageDTO;
import com.woniu.exception.DIYException;
import com.woniu.model.HisNursing;
import com.woniu.mapper.HisNursingMapper;
import com.woniu.param.HisNursingAddParam;
import com.woniu.param.HisNursingParam;
import com.woniu.param.PageParam;
import com.woniu.service.IHisNursingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.CopyBeanUtil;
import com.woniu.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-11
 */
@Service
public class HisNursingServiceImpl extends ServiceImpl<HisNursingMapper, HisNursing> implements IHisNursingService {
    @Resource
    private HisNursingMapper hisNursingMapper;
    @Resource
    private IHisNursingService hisNursingService;

    @Override
    public Result<PageDTO<HisNursingDto>> getAllHisNursing() {
        return null;
    }

    @Override
    public Result<HisNursingDto> addHisNursing(HisNursingAddParam hisNursingAddParam) {
        String str = "服务器异常，hisNursingAddParam为空";
        if (hisNursingAddParam==null){ new DIYException(-9999,str);}
        HisNursing hisNursing  = new HisNursing();
        BeanUtils.copyProperties(hisNursingAddParam,hisNursing);
        hisNursingMapper.insert(hisNursing);
        HisNursingDto hisNursingDto = new HisNursingDto();
        BeanUtils.copyProperties(hisNursing,hisNursingDto);
        return Result.bulid(hisNursingDto);
    }

    @Override
    public Result<Boolean> deleteHisNursingById(ArrayList<Integer> id) {
        String str = "服务器异常，id为空";
        if (id==null){new DIYException(-9999,str);}
        int i = hisNursingMapper.deleteBatchIds(id);
        return Result.bulid(i==0?new Boolean(false):new Boolean(true));
    }

    @Override
    public Result<HisNursingDto> updateHisNursing(HisNursingParam hisNursingParan) {
        String str = "服务器异常，hisNursingParan为空";
        if (hisNursingParan==null){new DIYException(-9999,str);}
        HisNursing hisNursing = new HisNursing();
        HisNursingDto hisNursingDto = new HisNursingDto();
        BeanUtils.copyProperties(hisNursingParan,hisNursing);
        BeanUtils.copyProperties(hisNursingParan,hisNursingDto);
        hisNursingMapper.updateById(hisNursing);
        return Result.bulid(hisNursingDto);
    }

    @Override
    public Result<PageDTO<HisNursingDto>> getHisNursingBy(HisNursingParam hisNursingParam) {
        if (hisNursingParam==null){new DIYException(-9999,"hisNursingParam为空");}
        QueryWrapper<HisNursing> wrapper = new QueryWrapper<>();
        if (hisNursingParam.getNursingId()!=null){
            wrapper.eq("nursing_id",hisNursingParam.getNursingId());
        }
        if (hisNursingParam.getHospitalRecordsId()!=null){
            wrapper.eq("hospital_records_id",hisNursingParam.getHospitalRecordsId());
        }
        if (hisNursingParam.getNurseId()!=null){
            wrapper.eq("nurse_id",hisNursingParam.getNurseId());
        }
        if (hisNursingParam.getNurseName()!=null){
            wrapper.eq("nurse_name",hisNursingParam.getNurseName());
        }
        if (hisNursingParam.getNursingTime()!=null){
            wrapper.eq("nursing_time",hisNursingParam.getNursingTime());
        }
        if (hisNursingParam.getNursingContent()!=null){
            wrapper.eq("nursing_content",hisNursingParam.getNursingContent());
        }
        if(hisNursingParam.getPageParam()==null){
            PageParam pageParam = new PageParam();
            pageParam.setPage(0L);
            pageParam.setPageSize(100L);
            hisNursingParam.setPageParam(pageParam);
        }
        Page<HisNursing> page = page(new Page<HisNursing>(hisNursingParam.getPageParam().getPage(),hisNursingParam.getPageParam().getPageSize()),wrapper);
        List<HisNursingDto> hisNursingDtos = CopyBeanUtil.copyBean(page.getRecords(), HisNursingDto.class);
        return Result.bulid(PageDTO.bulid(page,hisNursingDtos));
    }
}
